'''

Case Type： 数据库系统
Case Name： 事务运行时间超长
Create At:  2020/06/29
Owner    :  @li-xin12345
Descption:  1.修改配置文件中参数statement_timeout 2.重启使参数生效 3.在事务中执行pg_sleep函数，使事务超时 4.验证事务是否回滚

history：
created： 2020/06/28  created by @li-xin12345
'''

import os
import time
import _thread
import queue
import unittest
from yat.test import Node
from testcase.utils.CommonSH import *
from yat.test import macro
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import *

from testcase.utils.Constant import Constant


class PortOccupied(unittest.TestCase):
    sh_primy = CommonSH('PrimaryDbUser')
    sh_stadb = CommonSH('Standby1DbUser')
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log = Logger()
        self.log.info('------------Opengauss_Reliability_Dbsys_Case102.py start------------')
        self.rootNode = Node('PrimaryRoot')
        self.primaryNode = Node(node='PrimaryDbUser')
        self.SOURCE_PATH = macro.DB_ENV_PATH
        self.DN1_PATH = macro.DB_INSTANCE_PATH
        self.Constant = Constant()

    def test_gres_start(self):
        # 更改statement_timeout参数
        self.log.info('----------------------------更改statement_timeout参数-----------------------------')
        mod_msg = self.sh_primy.executeGsguc('reload', self.Constant.GSGUC_SUCCESS_MSG, 'statement_timeout=1000')
        self.assertTrue(mod_msg)

        # 开启事务后执行事务并且不提交
        self.log.info('----------------------------开启事务并执行pg_sleep函数----------------------------')
        sql_cmd = '''
                    start transaction;
                    create table test_zl1(a tinyint,b tinyint,c integer,d bigint);
                    select pg_sleep(6);
                  '''
        tran_msg = self.sh_primy.executDbSql(sql_cmd)
        self.log.info(sql_cmd)
        self.log.info(tran_msg)
        res2 = tran_msg.find('canceling statement due to statement timeout') > -1
        self.assertTrue(res2)

        # 验证事务是否回滚
        self.log.info('----------------------------验证事务是否回滚-----------------------------')
        sql_cmd = 'select * from test_zl1;'
        roll_msg = self.sh_primy.executDbSql(sql_cmd)
        self.log.info(sql_cmd)
        self.log.info(roll_msg)
        self.assertIn(self.Constant.NOT_EXIST, roll_msg)

    def tearDown(self):
        self.log.info('----------------------------后置处理-----------------------------')
        try:
            db_status = self.sh_primy.getDbClusterStatus('status')
            self.log.info(db_status)
            if db_status:
                pass
            else:
                is_started = self.sh_primy.startDbCluster()

                self.assertTrue(is_started)
                self.log.info(f'{is_started}')
                self.log.info(f'db_status: {is_started}')
        finally:
            mod_msg = self.sh_primy.executeGsguc('reload', self.Constant.GSGUC_SUCCESS_MSG,
                                                 'statement_timeout=2147483647')
            self.assertTrue(mod_msg)
        self.log.info('------------------reliability_dbsys102 执行完成------------------------------')
